package com.pan.admin.module.market.controller;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.channel.entity.LoanChannel;
import com.pan.admin.module.channel.entity.LoanChannelMarket;
import com.pan.admin.module.channel.service.LoanChannelMarketService;
import com.pan.admin.module.channel.service.LoanChannelService;
import com.pan.admin.module.market.entity.LoanMarket;
import com.pan.admin.module.market.entity.vo.RespMarketChannelFilter;
import com.pan.admin.module.market.entity.vo.RespMarketChannelFilterVO;
import com.pan.admin.module.market.service.LoanMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 贷款超市Controller
 *
 * @author ts
 * @date 2023-09-13
 */
@RestController
@RequestMapping("/product/market")
public class LoanMarketController extends BaseController {

    @Autowired
    private LoanMarketService loanMarketService;
    @Autowired
    private LoanChannelService loanChannelService;
    @Autowired
    private LoanChannelMarketService loanChannelMarketService;

    /**
     * 查询贷款超市列表
     */
    @PreAuthorize("@ss.hasPermi('product:market:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanMarket loanMarket) {
        startPage();
        List<LoanMarket> list = loanMarketService.list(loanMarket);
        return getDataTable(list);
    }

    /**
     * 获取贷款超市详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:market:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanMarketService.getById(id));
    }

    /**
     * 新增贷款超市
     */
    @PreAuthorize("@ss.hasPermi('product:market:add')")
    @Log(title = "贷款超市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanMarket loanMarket) throws Exception {
        return toAjax(loanMarketService.insertLoanMarket(loanMarket));
    }

    /**
     * 修改贷款超市
     */
    @PreAuthorize("@ss.hasPermi('product:market:edit')")
    @Log(title = "贷款超市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanMarket loanMarket) {
        return toAjax(loanMarketService.updateLoanMarket(loanMarket));
    }

    /**
     * 修改贷款超市的状态
     */
    @PreAuthorize("@ss.hasPermi('product:market:edit')")
    @Log(title = "贷款超市", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody LoanMarket loanMarket) {
        return toAjax(loanMarketService.updateStatus(loanMarket));
    }

    /**
     * 逻辑删除贷款超市
     */
    @PreAuthorize("@ss.hasPermi('product:market:remove')")
    @Log(title = "贷款超市", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(loanMarketService.remove(id));
    }

    /**
     *  贷超-屏蔽渠道列表
     */
    @PreAuthorize("@ss.hasPermi('product:market:list')")
    @GetMapping("/listFilterChannel")
    public AjaxResult listFilterChannel(@RequestParam(value = "marketId", required = false) Long marketId) {
        //  获取所有可选择渠道
        LoanChannel enableChannel = new LoanChannel();
        enableChannel.setSelectApiChannel(false);
        enableChannel.setStatus(1);
        List<LoanChannel> listChannel = loanChannelService.list(enableChannel);

        //  添加贷超
        if (marketId == null) {
            RespMarketChannelFilterVO respVO = new RespMarketChannelFilterVO();
            List<RespMarketChannelFilter> listOptional = RespMarketChannelFilterVO.ofByChannel(listChannel);
            respVO.setListOptional(listOptional);
            return success(respVO);
        }

        //  修改贷超

        //  获取已关联贷超渠道
        LoanChannelMarket loanChannelMarket = new LoanChannelMarket();
        loanChannelMarket.setLoanMarketId(marketId);
        loanChannelMarket.setStatus(1);
        List<LoanChannelMarket> listChannelMarket = loanChannelMarketService.list(loanChannelMarket);

        //  排重
        CollUtils.filter(listChannel, t -> {
            LoanChannelMarket find = CollUtils.findOne(listChannelMarket, optional -> Eq.object(t.getId(), optional.getLoanChannelId()));
            return ValidParam.isNotNull(find);
        });

        //  修改时候，可选择和已选择取反操作
        RespMarketChannelFilterVO respVO = new RespMarketChannelFilterVO();
        List<RespMarketChannelFilter> listOptional = RespMarketChannelFilterVO.ofByChannel(listChannel);
        List<RespMarketChannelFilter> listSelect = RespMarketChannelFilterVO.ofByChannelMarket(listChannelMarket);
        respVO.setListOptional(listSelect);
        respVO.setListSelected(listOptional);
        return success(respVO);
    }
}
